﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using BLL.Installer;
using BLL.Interface.OperatingSystem;
using BLL.Model.OperatingSystem;
using System.Data;
using BLL.DbAccess;
using BLL.Utility;
using Castle.Core.Logging;

namespace BLL.Implement.OperatingSystem
{
    public class OperatingSystemInfoImpl : IOperatingSystemInfo
    {
        public ILogger Logger { get; set; }
        private readonly DefaultContext _defaultContext;
        private readonly DbSet<OperatingSystemInfo> _operatingSystemInfoContext;

        public OperatingSystemInfoImpl()
        {
            _defaultContext = ContainerHelper.Instance.Container.Resolve<DefaultContext>();
            _operatingSystemInfoContext = _defaultContext.OperatingSystemInfoContext;
        }

        public bool AddOperatingSystemInfo(OperatingSystemInfo operatingSystemInfo)
        {
            _operatingSystemInfoContext.Add(operatingSystemInfo);
            _defaultContext.SaveChanges();
            Logger.InfoFormat("新增了一条记录：{0}", operatingSystemInfo);
            return true;
        }

        public bool UpdateOperatingSystemInfo(OperatingSystemInfo operatingSystemInfo)
        {
            _defaultContext.Entry(operatingSystemInfo).State = EntityState.Modified;
            _defaultContext.SaveChanges();
            Logger.InfoFormat("更新了一条记录：{0}", operatingSystemInfo);
            return true;
        }


        public bool DeleteOperatingSystemInfoById(int id)
        {
            var operatingSystemInfo = FindOperatingSystemInfoById(id, true);
            if (operatingSystemInfo == null)
            {
                Logger.FatalFormat("删除失败：没有找到编号为{0}的记录", id);
                return false;
            }
            _operatingSystemInfoContext.Remove(operatingSystemInfo);
            _defaultContext.SaveChanges();
            Logger.InfoFormat("删除了一条记录：{0}", operatingSystemInfo);
            return true;
        }

        public bool DeleteAllOperatingSystemInfo()
        {
            const string sql = "delete from OperatingSystemInfo";
            try
            {
                _defaultContext.Database.ExecuteSqlCommand(sql);
                return true;
            }
            catch
            {
                Logger.FatalFormat(string.Format("删除所有记录失败，SQL：{0}", sql));
                return false;
            }
        }

        public IList<OperatingSystemInfo> GetAllOperatingSystemInfo()
        {
            return _operatingSystemInfoContext.AsNoTracking().ToList();
        }

        public OperatingSystemInfo FindOperatingSystemInfoById(int id, bool tracking)
        {
            return tracking
                       ? _operatingSystemInfoContext.AsNoTracking().FirstOrDefault(p => p.Id == id)
                       : _operatingSystemInfoContext.Find(id);
        }

        public IList<OperatingSystemInfo> GetOperatingSystemInfoByFilter(Dictionary<string, string> condition, Tuple<string, string> sort, PageInfo page)
        {
            var query = _operatingSystemInfoContext.AsNoTracking().AsQueryable();

            if (condition != null && condition.Count > 0)
            {
                if (!string.IsNullOrWhiteSpace("Name"))
                {
                    string name = condition["Name"];
                    query = query.Where(q => q.Name.Contains(name));
                }
            }

            if (sort == null || string.IsNullOrWhiteSpace(sort.Item1) || string.IsNullOrWhiteSpace(sort.Item2))
            {
                sort = new Tuple<string, string>("Id", "asc");
            }

            query = query.OrderBy(sort.Item1, sort.Item2);

            if (page != null)
            {
                page.TotalRecords = query.Count();
                return query.Skip(page.PageSize * (page.CurrentPage - 1))
                                            .Take(page.PageSize).ToList();
            }
            return query.ToList();
        }

        public bool AddOperatingSystemInfo(IList<OperatingSystemInfo> operatingSystemInfoList)
        {
            try
            {
                foreach (var entry in operatingSystemInfoList)
                {
                    _operatingSystemInfoContext.Add(entry);
                }
                int count = _defaultContext.SaveChanges();
                Logger.InfoFormat("{0}条记录被插入。", count);
                return true;
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                return false;
            }
        }

        public bool DeleteOperatingSystemInfoById(IList<int> operatingSystemInfoList)
        {
            try
            {
                foreach (var id in operatingSystemInfoList)
                {
                    var entryToDelete = FindOperatingSystemInfoById(id, true);
                    _operatingSystemInfoContext.Remove(entryToDelete);
                }
                int count = _defaultContext.SaveChanges();
                Logger.InfoFormat("{0}条记录被删除。", count);
                return true;
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                return false;
            }
        }

    }

}
